package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.yljf.domain.Book;
import com.ruoyi.yljf.domain.BookChapter;
import com.ruoyi.yljf.domain.User;
import com.ruoyi.yljf.domain.vo.BookshelfVO;
import com.ruoyi.yljf.service.IBookService;
import com.ruoyi.yljf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.BookshelfMapper;
import com.ruoyi.yljf.domain.Bookshelf;
import com.ruoyi.yljf.service.IBookshelfService;

import javax.annotation.Resource;

/**
 * 书架Service业务层处理
 *
 * @author metaee
 * @date 2025-01-07
 */
@Service
public class BookshelfServiceImpl extends ServiceImpl<BookshelfMapper, Bookshelf> implements IBookshelfService {
    @Resource
    private BookshelfMapper bookshelfMapper;
    @Resource
    private IUserService userService;
    @Resource
    private IBookService bookService;

    /**
     * 查询书架列表
     *
     * @param bookshelf 书架
     * @return 书架
     */
    @Override
    public List<BookshelfVO> selectBookshelfList(Bookshelf bookshelf) {
        LambdaQueryWrapper<Bookshelf> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(bookshelf.getUserId())) {
            queryWrapper.eq(Bookshelf::getUserId, bookshelf.getUserId());
        }
        if (StringUtils.isNotNull(bookshelf.getBookId())) {
            queryWrapper.eq(Bookshelf::getBookId, bookshelf.getBookId());
        }
        List<Bookshelf> bookshelves = bookshelfMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(bookshelves)) {
            return Collections.emptyList();
        }
        List<BookshelfVO> vos = new ArrayList<>();
        List<Long> userIds = bookshelves.stream().map(Bookshelf::getUserId).collect(Collectors.toList());
        List<Long> bookIds = bookshelves.stream().map(Bookshelf::getBookId).collect(Collectors.toList());
        List<User> users = userService.listByIds(userIds);
        List<Book> books = bookService.listByIds(bookIds);
        bookshelves.forEach(item -> {
            BookshelfVO vo = new BookshelfVO(item);
            for (User user : users) {
                if (user.getUserId().equals(item.getUserId())) {
                    vo.setUserName(user.getUserName());
                    break;
                }
            }
            for (Book book : books) {
                if (book.getId().equals(item.getBookId())) {
                    vo.setBookName(book.getName());
                    break;
                }
            }
            vos.add(vo);
        });
        return vos;
    }

    @Override
    public int saveBookshelf(Bookshelf bookshelf) {
        if (isCanAdd(bookshelf.getUserId(), bookshelf.getBookId())) {
            return bookshelfMapper.insert(bookshelf);
        }
        return 0;
    }

    @Override
    public int updateBookshelf(Bookshelf bookshelf) {
        Bookshelf selectById = bookshelfMapper.selectById(bookshelf.getShelfId());
        if (Objects.equals(selectById.getBookId(), bookshelf.getBookId())) {
            return bookshelfMapper.updateById(bookshelf);
        }
        if (isCanAdd(bookshelf.getUserId(), bookshelf.getBookId())) {
            return bookshelfMapper.updateById(bookshelf);
        }
        return 0;
    }

    private boolean isCanAdd(Long userId, Long bookId) {
        Bookshelf isExist = bookshelfMapper.selectOne(Wrappers.lambdaQuery(Bookshelf.class).eq(Bookshelf::getUserId, userId).eq(Bookshelf::getBookId, bookId));
        if (isExist != null) {
            throw new ServiceException("书架中已存在该书籍");
        }
        return true;
    }
}
